package com.jxs.android.cmcc.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jxs.android.cmcc.boot.SelfApplication;

/**
 * @Des 报表结构
 * @Author jiangxs
 * @Des 2012-8-31 上午9:16:50
 */
public class TableHolder {

	static public interface Keys {
		public static final String ROW_SPLIT = "ξ";
		public static final String COLUMN_SPLIT = "ā";
	}

	static public interface Table {
		public static final String TABLE_NAME = "table_data_tab";

		public static final String MENU_ID = "menu_id";
		public static final String STAT_DATE = "statDate";
		public static final String TABLE_DATA = "table_data";

		public static final String[] COLUMNS = new String[] { MENU_ID, STAT_DATE, TABLE_DATA };
	}

	public static void creater(SQLiteDatabase db) {
		StringBuffer createSQL = new StringBuffer(" CREATE TABLE IF NOT EXISTS ");
		createSQL.append(Table.TABLE_NAME);
		createSQL.append(" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ");
		createSQL.append(Table.MENU_ID + " varchar(20),");
		createSQL.append(Table.STAT_DATE + " varchar(25),");
		createSQL.append(Table.TABLE_DATA + " text ");
		createSQL.append(" )");
		db.execSQL(createSQL.toString());
	}

	public static void saveOrUpdate(String menuId, String statDate, String data) {
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		String params[] = new String[] { menuId, statDate };
		cursor = contentResolver.query(TableProvider.CONTENT_URI, new String[] { Table.STAT_DATE }, Table.MENU_ID + "=? and " + Table.STAT_DATE + " =? ", params, null);
		ContentValues values = new ContentValues();

		values.put(Table.MENU_ID, menuId);
		values.put(Table.STAT_DATE, statDate);
		values.put(Table.TABLE_DATA, data);

		if (null != cursor && cursor.getCount() == 0) {
			contentResolver.insert(TableProvider.CONTENT_URI, values);
		} else {
			contentResolver.update(TableProvider.CONTENT_URI, values, Table.MENU_ID + "=? and " + Table.STAT_DATE + " =? ", params);
		}
		if (null != cursor) {
			cursor.close();
		}
	}

	public static List<String> getValues(String menu_id, String statDate) {
		ArrayList<String> results = new ArrayList<String>();
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		String params[] = new String[] { menu_id, statDate };
		cursor = contentResolver.query(TableProvider.CONTENT_URI, new String[] { Table.TABLE_DATA }, Table.MENU_ID + "=? and " + Table.STAT_DATE + " =? ", params, null);
		if (null != cursor && cursor.moveToFirst()) {
			String da = cursor.getString(cursor.getColumnIndex(Table.TABLE_DATA));
			for (String row : da.split(Keys.ROW_SPLIT)) {
				results.add(row);
			}
		}
		if (null != cursor) {
			cursor.close();
		}
		return results;
	}
}
